package com.shipwe.loans.ui.activity

import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.shipwe.adapter.ShipweOnItemChildClickListener
import com.shipwe.loans.R
import com.shipwe.loans.databinding.ActivityPointsBinding
import com.shipwe.loans.pro.ShipweActivity
import com.shipwe.loans.ui.adapter.UserPointsAdapter
import com.shipwe.loans.ui.presenter.UserPointsPresenter
import com.shipwe.loans.ui.view.IUserPointsView
import com.shipwe.loans.utils.event.AppEventData
import com.shipwe.loans.utils.event.AppEventPointType
import com.shipwe.loans.utils.helper.UserPointsHelper
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/15 16:07
 * @Annotation：用户积分列表
 */
class ActivityUserPoints: ShipweActivity(),IUserPointsView {
    lateinit var binding: ActivityPointsBinding
    private var presenter: UserPointsPresenter? = null

    var page = 1
    private var pointsAdapter: UserPointsAdapter? = null

    override fun getShipweRootView(): View {
        binding = ActivityPointsBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initShipweView() {
        rootViewBinding.rootLayout.setBackgroundResource(R.drawable.page_login_bg)
        setShipweTitleBarVisibility(View.VISIBLE)
        setShipweBackBtnRes(R.drawable.back_black)
        setShipweRightBtnRes(R.drawable.customer_black)
        setShipweTextTitle(getString(R.string.points_details))

        initUserPointsRefreshView()
        initUserPointsListView()
        binding.pointsCount.text = UserPointsHelper.getShipweUserPoints().toString()
    }

    override fun initShipweListener() {
        //客服
        rootViewBinding.rootCustomer.setOnClickListener {
            showCustomerDialog("PointsDetails Page")
            appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_CUSTOMER,"PointsDetails Page")
        }
    }

    override fun initShipweData() {
        presenter = UserPointsPresenter(this)
        showShipweLoading(isTOuchOut = false, isCancel = true)
        onUserPointsRefreshAction()
    }


    /* 初始化刷新 */
    private fun initUserPointsRefreshView() {
        //禁止加载
        binding.userPointsRefreshView.setEnableLoadMore(true)
        binding.userPointsRefreshView.setEnableAutoLoadMore(false)
        binding.userPointsRefreshView.setOnRefreshListener {
            onUserPointsRefreshAction()
        }
        binding.userPointsRefreshView.setOnLoadMoreListener {
            onUserPointsLoadMoreAction()
        }
    }

    /**
     * 初始化积分明细列表
     */
    private fun initUserPointsListView() {
//        binding.pointsListView.setHasFixedSize(true)
//        binding.pointsListView.isNestedScrollingEnabled = false
//        binding.pointsListView.isFocusableInTouchMode = false //不需要焦点
//        val leftDecoration = AppDensityUtil.dp2px(16f)
//        val topDecoration = AppDensityUtil.dp2px(16f)
//        val bottomDecoration = AppDensityUtil.dp2px(8f)
//        binding.pointsListView.addItemDecoration(LinearSpacingItemDecoration(leftDecoration,leftDecoration,topDecoration,bottomDecoration))
        binding.pointsListView.layoutManager = LinearLayoutManager(this)
        pointsAdapter = UserPointsAdapter(binding.pointsListView)
        binding.pointsListView.adapter = pointsAdapter
        binding.pointsListView.itemAnimator = DefaultItemAnimator()
        pointsAdapter?.setOnItemChildClickListener(ShipweOnItemChildClickListener { _, childView, position ->
            val item = pointsAdapter!!.getItem(position)
            when (childView.id) {
                R.id.itemLayout -> {//点击Item

                }
            }
        })
    }

    /**
     * 刷新
     */
    private fun onUserPointsRefreshAction(){
        page = 1
        presenter?.loadUserPointsListAction()
    }

    /**
     * 加载更多
     */
    private fun onUserPointsLoadMoreAction(){
        page++
        presenter?.loadUserPointsListAction()
    }

    override fun getPointsPage(): Int {
        return page
    }

    /**
     * 设置积分明细
     */
    override fun showUserPointsList(totalCount: Int, pointsList: ArrayList<JSONObject>) {
        if (page == 1){
            pointsAdapter?.data = pointsList
        } else {
            pointsAdapter?.addMoreData(pointsList)
        }

        if (pointsAdapter!!.itemCount >= totalCount){
            binding.userPointsRefreshView.finishLoadMoreWithNoMoreData()
        } else {
            binding.userPointsRefreshView.resetNoMoreData()
        }
    }

    override fun dismissShipweLoading() {
        super.dismissShipweLoading()
        binding.userPointsRefreshView.finishRefresh()
        binding.userPointsRefreshView.finishLoadMore()
    }

    override fun onShipwePublishEvent(event: AppEventData) {
        //刷新积分完成
        if (event.eventType == AppEventData.TYPE.SHIPWE_POINTS_REFRESHED){
            binding.pointsCount.text = UserPointsHelper.getShipweUserPoints().toString()
        }
    }

    override fun onStart() {
        super.onStart()
        appEventPointRequestAction(AppEventPointType.PAGE.SHIPWE_PAGE_POINTS_LIST_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        appEventPointRequestAction(AppEventPointType.PAGE.SHIPWE_PAGE_POINTS_LIST_END)
    }
}